package com.hxzy.controller;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxzy.common.annon.RequiredAdmin;
import com.hxzy.common.annon.RequiredMerchant;
import com.hxzy.common.constans.Add;
import com.hxzy.common.constans.Update;
import com.hxzy.common.controller.BaseController;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.util.WaimaiThreadLocal;
import com.hxzy.common.vo.R;
import com.hxzy.dto.MerchandiseClassSearchDTO;
import com.hxzy.entity.Employee;
import com.hxzy.entity.MerchandiseClass;
import com.hxzy.service.MerchandiseClassService;
import com.hxzy.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "商品分类API")
@RestController
@RequestMapping(value = "/api/merchclass")
public class MerchandiseClassController extends BaseController {

    @Autowired
    private MerchandiseClassService merchandiseClassService;


    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/data")
    public R search(MerchandiseClassSearchDTO merchandiseClassSearchDTO){

        super.startPageHelper();
        List<MerchandiseClass> arrList=this.merchandiseClassService.search(merchandiseClassSearchDTO);
        return super.pageToPageVO(arrList);
    }

    @RequiredMerchant
    @ApiOperation(value = "新增")
    @PostMapping
    public R add(@RequestBody @Validated MerchandiseClass merchandiseClass){

        //调用新增方法
        boolean result = this.merchandiseClassService.save(merchandiseClass);

        return super.toR(result);
    }


    @RequiredMerchant
    @ApiOperation(value = "修改")
    @PutMapping
    public R update(@RequestBody @Validated  MerchandiseClass merchandiseClass){
        LoginVO current = WaimaiThreadLocal.getCurrent();

        MerchandiseClass db = this.merchandiseClassService.getById(merchandiseClass.getId());
        if(db==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }

        //如果不是该商家的，是不允许修改的
        if(merchandiseClass.getMerchantId().intValue()!=current.getAccountVO().getId().intValue()){
            return R.build(AckCode.VALUE_NOT_CHANGE);
        }

        db.setName(merchandiseClass.getName());
        db.setPicture(merchandiseClass.getPicture());



        boolean result = this.merchandiseClassService.updateById(db);
        return super.toR(result);
    }


    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{id}")
    public R findById(@PathVariable(value = "id") Long id){
        LoginVO current = WaimaiThreadLocal.getCurrent();

        MerchandiseClass merchandiseClass = null;
        if(WaimaiThreadLocal.isSuperMan()){
            merchandiseClass=this.merchandiseClassService.selectByPrimaryKey(id,null);
        }else {
            merchandiseClass = this.merchandiseClassService.selectByPrimaryKey(id, current.getAccountVO().getId());
        }

        if(merchandiseClass==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }

        return R.okHasData(merchandiseClass);
    }


    @ApiOperation(value = "根据商家ID查询分类")
    @GetMapping(value = "/merchantId")
    public R findByMerchandiseId( ){
      List<MerchandiseClass> arr=  this.merchandiseClassService.findByMerchandiseId();
      return R.okHasData(arr);
    }

}
